package store

import (
	"fmt"
	"time"

	"gitee.com/youkelike/zlog"
	"golang.org/x/crypto/bcrypt"
)

var cryptedPwd, _ = bcrypt.GenerateFromPassword([]byte("123"), bcrypt.DefaultCost)
var userStore = map[string]User{
	"alex": {Username: "alex", Password: cryptedPwd},
	"bob":  {Username: "bob", Password: cryptedPwd},
}

type User struct {
	Username  string
	Password  []byte
	LoginedAt time.Time
}

func Users() User {
	return User{}
}

func (u User) Get(username string) (*User, error) {
	u, ok := userStore[username]
	if !ok {
		return nil, fmt.Errorf("user not exit")
	}

	return &u, nil
}

func (u User) Compare(pwd string) error {
	zlog.Errorw("%s", u.Password)
	return bcrypt.CompareHashAndPassword(u.Password, []byte(pwd))
}
